package editor.UI
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import game.manager.MapManager;
	import game.manager.TemplateTileManager;
	import starling.core.Starling;
	import starling.events.ResizeEvent;
	
	/**
	 * ...
	 * @author Kirill Pletnev
	 */
	public class TopEditorPanel extends TopEditorUI
	{	
		public static const OPEN_NEW_TILE_WINDOW:String = "openTileWindow";
		
		public function TopEditorPanel()
		{
		}
		
		public function init():void
		{
			btnSaveMap.addEventListener(MouseEvent.CLICK, onSaveMap);
			btnSaveTiles.addEventListener(MouseEvent.CLICK, onSaveTiles);
			btnCreateTile.addEventListener(MouseEvent.CLICK, onCreateTile);
			bg.width = Starling.current.viewPort.width;
			
			Starling.current.stage.addEventListener(ResizeEvent.RESIZE, resizeStage);
		}
		
		private function onSaveMap(event:MouseEvent):void
		{
			var fileReference:FileReference = new FileReference();
			
			var data:ByteArray = new ByteArray();
			MapManager.instance.serialize(data);
			fileReference.save(data, MapManager.instance.mapID + ".data");
		}
		
		private function onSaveTiles(event:MouseEvent):void
		{
			var fileReference:FileReference = new FileReference();
			
			var data:ByteArray = new ByteArray();
			TemplateTileManager.instance.serialize(data);
			fileReference.save(data, "tilesLib.data");
		}
		
		private function onCreateTile(event:MouseEvent):void
		{
			dispatchEvent(new Event(OPEN_NEW_TILE_WINDOW));
		}
		
		private function resizeStage(event:ResizeEvent):void
		{
			bg.width = event.width;
		}
	}
}